package edu.whut.mall.admin.controller;

import edu.whut.mall.admin.annotations.LogAnnotation;
import edu.whut.mall.admin.domain.InstitutionParams;
import edu.whut.mall.admin.domain.UserParams;
import edu.whut.mall.admin.service.IInstitutionService;
import edu.whut.mall.admin.vo.UserVo;
import edu.whut.mall.common.api.CommonPage;
import edu.whut.mall.common.api.CommonResult;
import edu.whut.mall.common.util.IpUtil;
import edu.whut.mall.model.Institution;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @program: d9lab-mall
 * @description: 机构控制器
 * @author: Wayne
 * @create: 2020-04-27 14:44
 **/
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/institution")
@RestController
@Api(value="机构管理器",tags = {"机构管理器"})
public class RestInstitutionController {

    private final IInstitutionService iInstitutionService;


    @ApiOperation("添加机构")
    @PostMapping(value = "/addOneInstitution")
    public CommonResult addOneInstitution(@Valid @RequestBody InstitutionParams institutionParams){
        int result = iInstitutionService.insertOneInstitution(institutionParams);
        if (result <= 0) {
            return CommonResult.failed("添加机构失败");
        }
        return CommonResult.success("添加机构成功");
    }

    @ApiOperation("为机构指定管理员")
    @PostMapping(value = "/assignAdminToInstitution")
    public CommonResult assignAdminToInstitution(HttpServletRequest httpServletRequest,
                                                 @RequestParam Long institutionId,
                                                 @Valid @RequestBody UserParams userParams){
        int result = iInstitutionService.assignAdmin2Institution(institutionId, IpUtil.getIpAddr(httpServletRequest), userParams);
        if (result <= 0) {
            return CommonResult.failed("指定管理员失败");
        }
        return CommonResult.success("指定管理员成功");
    }

    @ApiOperation("更新机构信息")
    @PostMapping(value = "/updateOneInstitution")
    public CommonResult updateOneInstitution(@Valid @RequestBody InstitutionParams institutionParams){
        int result = iInstitutionService.updateOneInstitution(institutionParams);
        if (result <= 0) {
            return CommonResult.failed("更新机构信息失败");
        }
        return CommonResult.success("更新机构信息成功");
    }

    @ApiOperation("获取机构管理员信息")
    @GetMapping(value = "/getAdminInfoByInstitutionId")
    public CommonResult getAdminInfoByInstitutionId(@NotNull @RequestParam Long institutionId){
        List<UserVo> userVoList = iInstitutionService.listUserVosByInstitutionId(institutionId);
        if (userVoList != null) {
            return CommonResult.success(userVoList,"查询管理员成功");
        }
        return CommonResult.failed("暂无管理员信息,需要进行分配");
    }

    @ApiOperation("获取机构列表")
    @GetMapping(value = "/listInstitutionVo")
    public CommonResult listInstitutionVo(@RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                          @RequestParam(value="size",defaultValue ="10") Integer size){
        CommonPage<Institution> institutionVoPage = iInstitutionService.listInstitutionVos(pageNum, size);
        if (institutionVoPage != null) {
            return CommonResult.success(institutionVoPage,"获取机构列表成功");
        }
        return CommonResult.failed("获取机构列表失败");
    }
}
